// import Vue from 'vue';
// import Vuex from 'vuex';
'use strict';

import store from './store'

export function hideFooter(footer) {
    footer.style.transform = 'translateY(50px)';
    footer.style.transition = 'all 0.8s'; //js功能的实现隐藏效果
}
export function showFooter(footer) {
    footer.style.transform = 'translateY(0px)';
    footer.style.transition = 'all 0s'; //js功能的实现隐藏效果
}

//判断当前年份是否是闰年(闰年2月份有29天，平年2月份只有28天)
export function isLeap(year) {
    return year % 4 == 0 ? (year % 100 != 0 ? 1 : (year % 400 == 0 ? 1 : 0)) : 0;
}

//手动设置日历
export function setCalendar(y, m, d) {
    var m = m - 1;
    var i, k, html,
        firstday = new Date(y, m, 1), //获取当月的第一天
        dayOfWeek = firstday.getDay(), //判断第一天是星期几(返回[0-6]中的一个，0代表星期天，1代表星期一，以此类推)
        days_per_month = new Array(31, 28 + isLeap(y), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31), //创建月份数组
        str_nums = Math.ceil((dayOfWeek + days_per_month[m]) / 7); //确定日期表格所需的行数
    html = "<table cellspacing='3' class='table-calendar'><tr><th>日</th><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th></tr>"; //打印表格第一行(显示星期)
    var str = "";
    for (i = 0; i < str_nums; i += 1) { //二维数组创建日期表格
        str += '<tr>';
        for (k = 0; k < 7; k++) {
            var idx = 7 * i + k; //为每个表格创建索引,从0开始
            var date = idx - dayOfWeek + 1; //将当月的1号与星期进行匹配
            (date <= 0 || date > days_per_month[m]) ? date = ' ': date = idx - dayOfWeek + 1; //索引小于等于0或者大于月份最大值就用空表格代替
            date == d ? str += '<td><span>' + date + '</span></td>' : str += '<td ><span>' + date + '</span></td>'; //高亮显示当天
        }
        str += '</tr>';
    }
    html = html + str + '</table>';
    var div = document.createElement("div");
    div.classList.add("cal-wrapper")
    div.innerHTML = html;
    return div;
}


export function getToday(date, year, month, day) {
    var year = year ? year : date.getFullYear();
    var month = month ? month : date.getMonth() + 1;
    var day = day ? day : date.getDate();
    return {
        year: year,
        month: month,
        day: day
    }
}
//今天格式化字符串"2016-9-12"
export function today(date) {
    var defaultDate = date != null ? new Date(date) : new Date();
    var year = defaultDate.getFullYear();
    var month = defaultDate.getMonth() + 1;
    var day = defaultDate.getDate();
    return year + '-' + month + '-' + day
}
// 格式化日期转成"2017年6月"
export function ForToday(date) {
    var defaultDate = date != null ? new Date(date) : new Date();
    var year = defaultDate.getFullYear();
    var month = defaultDate.getMonth() + 1;
    var day = defaultDate.getDate();
    return year + '年' + month + '月'
}
export function Formonth(date) {
    var defaultDate = date != null ? new Date(date) : new Date();
    var year = defaultDate.getFullYear();
    var month = defaultDate.getMonth() + 1;
    var day = defaultDate.getDate();
    return month + '月' + day + '日';
}
// 时间戳转格式化时间
Date.prototype.Format = function(fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

//比较两个日期大小
export function compareDate(date1, date2) {
    if (date1 === null || date2 === null) {
        return ture;
    }
    //date1为字符串转换成,分隔的字符串
    var t1 = date1.getTime();
    var t2 = date2.getTime();;
    return (t1 >= t2) ? true : false;
};

// 判断一个月有几周
export function getWeeks(y, m) {
    var fd = new Date(y, m - 1, 1)
    var fday = fd.getDay();
    var fwd = 7 - fday;
    var weeks = 0;
    fd.setDate(fwd);
    while (fd <= new Date(y, m, 1)) {
        fd.setDate(fd.getDate() + 7);
        if (fd.getMonth() != m || (fd.getMonth() == m && fd.getDate() < 7)) weeks++;
    }
    // alert(weeks);
    return weeks;
}

export function getWeek(str) {
    // 将字符串转为标准时间格式
    str = Date.parse(str);
    str = new Date(str);
    // 先计算出该日期为第几周
    let week = Math.ceil(str.getDate() / 7);
    let month = str.getMonth() + 1;
    // 判断这个月前7天是周几，如果不是周一，则计入上个月
    if (str.getDate() < 7) {
        if (str.getDay() !== 1) {
            week = 5;
            month = str.getMonth();
        }
    }
    return `${month}-${week}`;
}

//获得某周的起始日期      
export function getWeekStartAndEnd(AddWeekCount, curDate) {
    //起止日期数组    
    var startStop = new Array();
    //一天的毫秒数    
    var millisecond = 1000 * 60 * 60 * 24;
    //获取当前时间    
    var currentDate = new Date(curDate);
    //相对于当前日期AddWeekCount个周的日期
    currentDate = new Date(currentDate.getTime() + (millisecond * 7 * AddWeekCount));
    //返回date是一周中的某一天
    var week = currentDate.getDay();
    //返回date是一个月中的某一天    
    var month = currentDate.getDate();
    //减去的天数    
    var minusDay = week != 0 ? week - 1 : 6;
    //获得当前周的第一天    
    var currentWeekFirstDay = new Date(currentDate.getTime() - (millisecond * minusDay));
    //获得当前周的最后一天
    var currentWeekLastDay = new Date(currentWeekFirstDay.getTime() + (millisecond * 6));
    //添加至数组    
    startStop.push(Formonth(currentWeekFirstDay));
    startStop.push(Formonth(currentWeekLastDay));
    return startStop;
}


//失败的回调
export function errFun() {
    store.dispatch('HIDDEN_LOADING_SHOW');
}


// 刷新title ios 微信必须要如此刷新
export function refreshTitle(title) {
    const ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf('iphone') > -1 && ua.indexOf('micromessenger') > -1) {
        setTimeout(function() {
            document.title = title;
            var iframe = document.createElement('iframe');
            iframe.style.visibility = 'hidden';
            iframe.style.width = '1px';
            iframe.style.height = '1px';
            iframe.src = '/favicon.ico'; // 这里
            iframe.onload = function() {
                setTimeout(function() {
                    document.body.removeChild(iframe);
                }, 0);
            };
            document.body.appendChild(iframe);
        }, 0);
        return;
    }
    document.title = title;
}

//绘制canvas
export function setRange(id, value, r, color, width) {
    // 根据ID获取对应的元素
    const CANVAS = document.getElementById(id);
    // 设置横坐标
    const X = CANVAS.width / 2;
    // 设置纵坐标
    const Y = CANVAS.height / 2;
    // 设置半径
    const RADIUS = r;
    // 设置圆形的生成方向，false 表示为顺时针
    const COUNTER_CLOCKWISE = false;
    // 设置绘制进度条圆形的宽度
    const PROGRESS_LINE_WIDTH = width;
    // 设置边界圆形的宽度
    const COMMON_LINE_WIDTH = 1;
    // 设置开始角度
    const START_ANGLE = 0;
    // 设置结束角度
    const END_ANGLE = Math.PI * 2;
    // 封装绘制圆形的过程，方便重复是用
    function drawCycle(canvasElem, positionX, positionY, radius, startAngle, endAngle, couterClockwise, lineWidth, color) {
        let cycle = canvasElem.getContext('2d');
        cycle.lineWidth = lineWidth;
        cycle.beginPath();
        cycle.arc(positionX, positionY, radius, startAngle, endAngle, couterClockwise);
        cycle.strokeStyle = color;
        cycle.stroke();
        cycle.lineCap = 'round';
    }

    // 初始化边框
    // 内边框
    drawCycle(CANVAS, X, Y, RADIUS - PROGRESS_LINE_WIDTH / 2, START_ANGLE, END_ANGLE, COUNTER_CLOCKWISE, COMMON_LINE_WIDTH, color);
    // 外边框
    drawCycle(CANVAS, X, Y, RADIUS + PROGRESS_LINE_WIDTH / 2, START_ANGLE, END_ANGLE, COUNTER_CLOCKWISE, COMMON_LINE_WIDTH, "transparent");
    // 实时清空旧的图形
    let clearRECT = CANVAS.getContext('2d');
    clearRECT.clearRect(X - RADIUS - PROGRESS_LINE_WIDTH, Y - RADIUS - PROGRESS_LINE_WIDTH, RADIUS * 2 + PROGRESS_LINE_WIDTH * 2, RADIUS * 2 + PROGRESS_LINE_WIDTH * 2);
    // 绘制进度条
    drawCycle(CANVAS, X, Y, RADIUS - PROGRESS_LINE_WIDTH / 2, START_ANGLE, END_ANGLE, COUNTER_CLOCKWISE, COMMON_LINE_WIDTH);
    drawCycle(CANVAS, X, Y, RADIUS + PROGRESS_LINE_WIDTH / 2, START_ANGLE, END_ANGLE, COUNTER_CLOCKWISE, COMMON_LINE_WIDTH);
    // 根据输入值绘制进度条
    drawCycle(CANVAS, X, Y, RADIUS, START_ANGLE - Math.PI / 2, value - Math.PI / 2, COUNTER_CLOCKWISE, PROGRESS_LINE_WIDTH, color);
};

// 时间格式化 ---- 计划的时间
export function forPdate(arr) {
    arr.real.time = arr.real.time > 60 ? parseInt(arr.real.time / 60) + '分钟' : arr.real.time + '秒';
    arr.time = arr.time > 60 ? parseInt(arr.time / 60) + '分钟' : arr.time + '秒';
    return arr;
}

// 时间格式化 --- 自选课程的时间和卡路里
export function forSdate(arr) {
    var i = 0;
    for (var i = 0; i < arr.length; i++) {
        if (arr[i].finished) {
            // 格式化real时间
            arr[i].real.time = arr[i].real.time > 60 ? parseInt(arr[i].real.time / 60) + '分钟' : arr[i].real.time + '秒';
        } else {
            // 如果没有完成返回的应该是
        }
        arr[i].time = arr[i].time > 60 ? parseInt(arr[i].time / 60) + '分钟' : arr[i].time + '秒';
    }
    return arr;
}

// 时间格式化 ---- 累计的时间
export function forCtime(arr) {
    arr = arr > 60 ? parseInt(arr / 60) + '分钟' : arr + '秒';
    return arr;
}

export function forACdate(arr) {
    for (var i = 0; i < arr.length; i++) {
        arr[i].duration = arr[i].duration > 60 ? parseInt(arr[i].duration / 60) + '分钟' : arr[i].duration + '秒';
        arr[i].dynamicKcal = parseInt(arr[i].dynamicKcal);
        arr[i].kcal = parseInt(arr[i].kcal);
    }
    return arr;
}